package com.app.utils.bean;

import java.util.Objects;

public class MFile {
    private String parent,md5,name;
    private Long lastModified,size;

    /**
     * 格式：文件名|md5|最后修改时间|文件大小
     */
    public MFile(String parent,String infoStr) {
        this.parent=parent.replaceAll("/+|\\\\+","/");
        init(infoStr);
    }

    /**
     * 文件夹
     * @param path 必须以“/”开头
     * @param lastModified
     */
    public MFile(String path,Long lastModified) {
        this.lastModified=lastModified;
        path=path.replaceAll("/+|\\\\+","/");
        int pos = path.lastIndexOf("/");
        this.parent = path.substring(0,pos);
        this.name = path.substring(pos+1);
    }

    private void init(String infoStr) {
        String[] sp = infoStr.split("\\|");
        if (sp.length==4){
            this.name=sp[0];
            this.md5=sp[1];
            this.lastModified=Long.parseLong(sp[2]);
            this.size=Long.parseLong(sp[3]);
        }else if (sp.length==2){
            this.name=sp[0];
            this.lastModified=Long.parseLong(sp[1]);
        }
    }

    public String getPath(){
        return parent+"/"+name;
    }

    public String getParent() {
        return parent;
    }

    public String getName() {
        return name;
    }

    public String getMd5() {
        return md5;
    }

    public Long getLastModified() {
        return lastModified;
    }

    public Long getSize() {
        return size;
    }

    public boolean isFile() {
        return Objects.isNull(md5);
    }
    public static boolean judgePath(String path){
        if (path==null)return false;
        path=path.replace("\\","/");
        // 开头是否标准，是否存在 ./
        if (path.matches("[0-9]+"))return true;
        if (path.matches("[0-9]*/.*")&&!path.matches(".*(\\./)+.*"))
            return !path.matches("^.*[:<>|\\*\\?]+.*$");//是否存在其它不允许字符
        return false;
    }
}
